package com.dynasty;

/**
 * @description 数组类
 * Created by wangchao12 on 2019/7/8 14:13.
 */

public class Array {
    /**
     * 方法的功能描述：给定一个整数数组(至少包含一个数)，找到一个具有最大和的连续子数组，返回其最大和
     * 输入：[-2,1,-3,4,-1,2,1,-5,4]
     * 输出：6
     * 解释：连续子数组[4，-1,2,1]的和最大，值为6。h
     *
     * @param array
     * @return
     */
    private int maxSubAaary(int[] array) {
        int result = array[0];
        int sum = 0;
        for (int num : array) {
            if (sum > 0) {
                sum += num;
            } else {
                sum = num;
            }
            result = Math.max(result, sum);
        }
        return result;
    }

    public static void main(String[] args) {
        Array a = new Array();
        int[] array = new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4};
        System.out.println(a.maxSubAaary(array));
    }
}
